package hk.m4s.lr.repair.test.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import framework.common.Constant;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.JiangliModel;
import hk.m4s.lr.repair.test.model.MyOrderModel;
import hk.m4s.lr.repair.test.model.MyWaiteOrderModel;
import hk.m4s.lr.repair.test.service.AccountSerive;
import hk.m4s.lr.repair.test.ui.goods.PayMoneyActivity;
import hk.m4s.lr.repair.test.ui.user.LookUnprocessedOrderActivity;
import hk.m4s.lr.repair.test.ui.user.MyoderActivity;
import hk.m4s.lr.repair.test.ui.user.OrderSmSActivity;
import hk.m4s.lr.repair.test.ui.user.TuiMoneyActivity;
import hk.m4s.lr.repair.test.utils.AppTools;
import hk.m4s.lr.repair.test.utils.MoneyTool;
import hk.m4s.lr.repair.test.views.listview.InnerListView;
import okhttp3.Response;

/**
 * @author
 */
public class MyOderAdapter extends BaseAdapter {
    private MyoderActivity context;
    public List<MyOrderModel.ListBean> items;
    private List<MyWaiteOrderModel> myjiangLiModelList;
    private ItemMyAllOrderAdapter adapter;
    int selectNums = 0;

    public MyOderAdapter(MyoderActivity context,
                         List<MyOrderModel.ListBean> items) {
        this.context = context;
        this.items = items;
    }

    public int getCount() {
        return items.size();
    }

    public Object getItem(int position) {
        return getView(position, null, null);
    }

    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.list_item_my_waite_money, null);
            holder = new ViewHolder();
            holder.order_shop_time = (TextView) convertView.findViewById(R.id.order_shop_time);
            holder.order_shop_totlaprice = (TextView) convertView.findViewById(R.id.order_shop_totlaprice);
            holder.cancle_order = (Button) convertView.findViewById(R.id.cancle_order);
            holder.pay_btn = (Button) convertView.findViewById(R.id.pay_btn);
            holder.header = (RelativeLayout) convertView.findViewById(R.id.header);
            holder.itemList = (InnerListView) convertView.findViewById(R.id.itemList);
            holder.order_shop_state = (TextView) convertView.findViewById(R.id.order_shop_state);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.cancle_order.setTag(position);
        holder.pay_btn.setTag(position);
        holder.header.setTag(position);

        MyOrderModel.ListBean model = items.get(position);
        if (model.getStatus().equals("1")) {//待付款
            holder.pay_btn.setVisibility(View.VISIBLE);
            holder.order_shop_state.setText("等待付款");
            holder.cancle_order.setText("取消订单");
            holder.pay_btn.setText("立即支付");
            holder.order_shop_time.setText("下单时间:" + AppTools.getStrTime(model.getAdd_time()));
        } else if (model.getStatus().equals("2")) {//待发货
            holder.pay_btn.setVisibility(View.VISIBLE);
            holder.order_shop_state.setText("等待发货");
            holder.cancle_order.setText("申请售后");
            holder.pay_btn.setText("催发货");
            holder.order_shop_time.setText("订单编号:" + model.getOrder_number());
        } else if (model.getStatus().equals("3")) {//待收货
            holder.pay_btn.setVisibility(View.VISIBLE);
            holder.order_shop_state.setText("等待收货");
            holder.order_shop_time.setText("订单编号:" + model.getOrder_number());
            holder.cancle_order.setText("确认收货");
            holder.pay_btn.setText("查看物流");
        } else if (model.getStatus().equals("4")) {//已完成
            holder.order_shop_state.setText("交易成功");
            holder.cancle_order.setText("删除订单");
            holder.pay_btn.setVisibility(View.GONE);
            holder.order_shop_time.setText("订单编号:" + model.getOrder_number());
        } else if (model.getStatus().equals("6")) {//待评价
            holder.order_shop_state.setText("等待评价");
            holder.cancle_order.setText("删除订单");
            holder.pay_btn.setVisibility(View.GONE);
            holder.order_shop_time.setText("订单编号:" + model.getOrder_number());
        }
        holder.header.setVisibility(View.VISIBLE);


        holder.order_shop_totlaprice.setText(MoneyTool.getLocalMoney(model.getActual_money()));
        if (model.getDetail() != null) {
            myjiangLiModelList = new ArrayList<>();
            for (MyOrderModel.ListBean.DetailBean detailBean : model.getDetail()) {
                MyWaiteOrderModel waiteOrderModel = new MyWaiteOrderModel();
                waiteOrderModel.setTime(AppTools.getStrTime(detailBean.getAddTime()));
                waiteOrderModel.setGoods_id(detailBean.getGoods_id());
                waiteOrderModel.setName(detailBean.getGoods_name());
                waiteOrderModel.setPrice(detailBean.getGoods_price());
                waiteOrderModel.setSize(detailBean.getGoods_color());
                waiteOrderModel.setBuyNum(detailBean.getGoods_num());
                waiteOrderModel.setState(detailBean.getStatus());
                waiteOrderModel.setImg(detailBean.getImg());
                waiteOrderModel.setDetails(model.getDetails());
                waiteOrderModel.setOrderId(model.getOrder_id());
                myjiangLiModelList.add(waiteOrderModel);
            }
            adapter = new ItemMyAllOrderAdapter(context, myjiangLiModelList,model.getStatus());
            holder.itemList.setAdapter(adapter);
        }
        holder.header.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {
                int num = (Integer) v.getTag();
                MyOrderModel.ListBean model = items.get(num);
                Intent aboutIntent=null;
                if(model.getStatus().equals("6")){
                   // aboutIntent = new Intent(context, LookEnOrderActivity.class);
                }else {
                    aboutIntent = new Intent(context, LookUnprocessedOrderActivity.class);
                }
                aboutIntent.putExtra("orderId", model.getOrder_id());
                aboutIntent.putExtra("statuts", model.getStatus());
                if (model.getPayInfo().getOilMoney() != null) {
                    aboutIntent.putExtra("oilMoney", model.getPayInfo().getOilMoney());
                }
                context.startActivity(aboutIntent);
            }
        });

        holder.cancle_order.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {
                int num = (Integer) v.getTag();
                MyOrderModel.ListBean myOrderModel = items.get(num);
                if (myOrderModel.getStatus().equals("1")) {
                    //updateOrderStatus(myOrderModel.getOrder_id());
                } else if (myOrderModel.getStatus().equals("2")) {
                    Intent intent = new Intent(context, TuiMoneyActivity.class);
                    intent.putExtra("model", myOrderModel);
                    intent.putExtra("orderId", myOrderModel.getOrder_id());
                    intent.putExtra("type", "1");
                    context.startActivity(intent);
                }else if(myOrderModel.getStatus().equals("3")){
                    //updateOrderStatus(myOrderModel.getOrder_id());
                }else if(myOrderModel.getStatus().equals("4")){
                    //updateOrderStatus(myOrderModel.getOrder_id());
                } else if(myOrderModel.getStatus().equals("6")){
                    //deleteOrder(myOrderModel.getOrder_id());
                }else {

                }

            }
        });
        holder.pay_btn.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {
                int num = (Integer) v.getTag();
                MyOrderModel.ListBean detailBean = items.get(num);
                if (detailBean.getStatus().equals("1")) {
                    Intent intent = new Intent(context, PayMoneyActivity.class);
                    intent.putExtra("totalMoney", detailBean.getPayInfo().getPaymentMoney());
                    intent.putExtra("yunMoney", detailBean.getPayInfo().getFreight());
                    intent.putExtra("actual_money", detailBean.getPayInfo().getGoodsMoney());
                    intent.putExtra("orderId", detailBean.getPayInfo().getOrderNumber());
                    if (detailBean.getPayInfo().getOilMoney() != null) {
                        intent.putExtra("show", "2");
                        intent.putExtra("oilMoney", detailBean.getPayInfo().getOilMoney());
                    } else {
                        intent.putExtra("show", "1");
                    }
                    intent.putExtra("goPay", "2");
                    context.startActivity(intent);
                }else if(detailBean.getStatus().equals("2")){
                    //pushcargo(detailBean.getPayInfo().getOrderNumber());
                }else if(detailBean.getStatus().equals("3")){
                    Intent intent = new Intent(context, OrderSmSActivity.class);
                    intent.putExtra("totalMoney",detailBean.getPayInfo().getPaymentMoney());
                    intent.putExtra("yunMoney",detailBean.getPayInfo().getFreight());
                    intent.putExtra("actual_money",detailBean.getPayInfo().getGoodsMoney());
                    intent.putExtra("orderId", detailBean.getPayInfo().getOrderNumber());
                    if (detailBean.getPayInfo().getOilMoney()!= null) {
                        intent.putExtra("show", "2");
                        intent.putExtra("oilMoney", detailBean.getPayInfo().getOilMoney());
                    } else {
                        intent.putExtra("show", "1");
                    }
                    intent.putExtra("goPay", "2");
                    context.startActivity(intent);
                }


            }
        });
        return convertView;
    }

    private class ViewHolder {
        TextView order_shop_time, order_shop_totlaprice, order_shop_state;
        Button cancle_order, pay_btn;
        private RelativeLayout header;
        InnerListView itemList;
    }
//    public void updateOrderStatus(String orderId) {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        parmas.put("orderId",orderId);
//        parmas.put("status","6");
//        AccountSerive.updateOrderStatus(context, parmas, new ResponseCallback<JiangliModel>() {
//            @Override
//            public void onSuccess(JiangliModel model) {
//                context.update();
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
//
//    }
//    public void deleteOrder(String orderId) {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("orderId", orderId);
//        AccountSerive.deleteOrder(context, parmas, new ResponseCallback<GoodsCarModel>() {
//            @Override
//            public void onSuccess(GoodsCarModel model) {
//                context.update();
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
//
//    }
//    public void pushcargo(String orderId) {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("orderid", orderId);
//        AccountSerive.pushcargo(context, parmas, new ResponseCallback<GoodsCarModel>() {
//            @Override
//            public void onSuccess(GoodsCarModel model) {
//                ToastUtil.toast(context,"提醒商家成功");
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
//
//    }
}
